package com.example.ytyproject.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ytyproject.config.SecurityUtil;
import com.example.ytyproject.config.exception.AppException;
import com.example.ytyproject.entity.BillFare;
import com.example.ytyproject.entity.Menu;
import com.example.ytyproject.entity.Role;
import com.example.ytyproject.entity.vo.BillCategoryVo;
import com.example.ytyproject.entity.vo.R;
import com.example.ytyproject.mapper.BillFareMapper;
import com.example.ytyproject.mapper.ShopsBaseMapper;
import com.example.ytyproject.service.BillFareService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 菜单基本表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-04-16
 */
@Service
public class BillFareServiceImpl extends ServiceImpl<BillFareMapper, BillFare> implements BillFareService {

    @Autowired
    private ShopsBaseMapper shopsBaseMapper;

    @Override
    public R listByPage(BillFare param) {
        QueryWrapper<BillFare> queryWrapper = new QueryWrapper<BillFare>();
        LambdaQueryWrapper<BillFare> lambda = queryWrapper.lambda();
        lambda.orderByDesc(BillFare::getCreateDate);
        if (StringUtils.isNotEmpty(param.getShopsId())) {
            lambda.eq(BillFare::getShopsId, param.getShopsId());
        }
        if (StringUtils.isNotEmpty(param.getCategoryType())) {
            lambda.eq(BillFare::getCategoryType, param.getCategoryType());
        }
        if (StringUtils.isNotEmpty(param.getFoodName())) {
            lambda.eq(BillFare::getFoodName, param.getFoodName());
        }
        Page<BillFare> page = this.page(new Page<>(param.getPageNo(), param.getPageSize()), queryWrapper);
        return R.ok(page.getRecords(), page.getTotal());
    }

    @Override
    public void addData(BillFare param) {
        if (StringUtils.isEmpty(param.getShopsId())) {
            throw new AppException("请绑定商铺！");
        }
        if (StringUtils.isEmpty(param.getFoodName())) {
            throw new AppException("请输入商品名称！");
        }
        BillFare f = getBillFare(param.getShopsId(), param.getFoodName());
        if (f != null) {
            throw new AppException("该菜单已经存在，请勿重复提交！");
        }
        if (StringUtils.isEmpty(param.getCategoryType())) {
            param.setCategoryType("主食"); // 默认为主食
        }
        param.setCreateDate(LocalDateTime.now())
                .setCreateBy(SecurityUtil.getUserId())
                .setUpdateDate(LocalDateTime.now())
                .setUpdateBy(SecurityUtil.getUserId())
                .setDeleted(false);
        this.save(param);
    }

    @Override
    public BillFare getBillFare(String shopId, String foodName) {
        QueryWrapper<BillFare> queryWrapper = new QueryWrapper<BillFare>();
        LambdaQueryWrapper<BillFare> lambda = queryWrapper.lambda();
        lambda.eq(BillFare::getShopsId, shopId).eq(BillFare::getFoodName, foodName);
        return this.getOne(queryWrapper);
    }

    @Override
    public List<BillCategoryVo> billCategory() {
        List<BillFare> list = this.list();
        if (list.size() == 0) {
            return Lists.newArrayList();
        }
        List<BillCategoryVo> res = new ArrayList<>();
        List<String> shopIds = list.stream().map(BillFare::getShopsId).distinct().collect(Collectors.toList());
        for (String shopId : shopIds) {
            List<String> data = list.stream().filter(e -> e.getShopsId().equals(shopId)).map(BillFare::getCategoryType).distinct().collect(Collectors.toList());
            List<BillCategoryVo> children = new ArrayList<>();
            for (String category : data) {
                BillCategoryVo c = new BillCategoryVo();
                c.setShopsId(shopId).setLabel(category);
                children.add(c);
            }
            BillCategoryVo c = new BillCategoryVo();
            c.setShopsId(shopId).setLabel(shopsBaseMapper.selectById(shopId).getShopName()).setChildren(children);
            res.add(c);
        }
        return res;
    }

    @Override
    public void updateData(BillFare param) {

    }
}
